package com.vehicle.biz.Impl;

import cn.hutool.core.collection.CollectionUtil;
import com.vehicle.biz.CameraBiz;
import com.vehicle.domains.bo.QueryCameraBo;
import com.vehicle.domains.entity.CameraEntity;
import com.vehicle.domains.vo.BasePage;
import com.vehicle.domains.vo.req.camera.*;
import com.vehicle.domains.vo.res.camera.CameraRes;
import com.vehicle.enums.WorkModelEnum;
import com.vehicle.exception.VehicleAccessException;
import com.vehicle.service.CameraService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CameraBizImpl implements CameraBiz {

    @Resource
    private CameraService cameraService;

    @Override
    public void addCamera(AddCameraReq req) {
        CameraEntity cameraEntity = new CameraEntity();
        BeanUtils.copyProperties(req, cameraEntity);

        cameraService.addCamera(cameraEntity);
    }

    @Override
    public void updateCamera(UpdateCameraReq req) {
        checkCameraExist(req.getId());

        CameraEntity updateEntity = new CameraEntity();
        BeanUtils.copyProperties(req, updateEntity);
        cameraService.updateCamera(updateEntity);
    }

    @Override
    public void deleteCamera(Long id) {
        checkCameraExist(id);

        List<CameraEntity> cameraEntityList = cameraService.queryByAssociationCameraId(id);

        if (CollectionUtil.isNotEmpty(cameraEntityList)) {
            throw VehicleAccessException.DELETE_ERROR.newInstance("有绑定相机未删除");
        }

        cameraService.deleteCamera(id);

    }

    @Override
    public CameraRes getCameraById(Long id) {
        CameraEntity cameraEntity = checkCameraExist(id);

        CameraRes cameraRes = new CameraRes();
        BeanUtils.copyProperties(cameraEntity, cameraRes);
        return cameraRes;
    }

    @Override
    public BasePage<CameraRes> list(CameraListReq req) {
        Page<CameraEntity> page = PageHelper.startPage(req.getPageNo(), req.getPageSize());

        QueryCameraBo queryCameraBo = new QueryCameraBo();
        BeanUtils.copyProperties(req, queryCameraBo);


        List<CameraEntity> cameraEntityList = cameraService.selectCameraList(queryCameraBo);
        List<CameraRes> list = cameraEntityList.stream().map(cameraEntity -> {
            CameraRes cameraRes = new CameraRes();
            BeanUtils.copyProperties(cameraEntity, cameraRes);

            return cameraRes;
        }).toList();
        return BasePage.generateResult(page.getTotal(), page.getPages(), list);
    }

    @Override
    public void bindCamera(BindCameraReq req) {
        bindCameraCheck(req);

        CameraEntity updateEntity = new CameraEntity();
        updateEntity.setId(req.getId());
        updateEntity.setAssociationCameraId(req.getCameraId());

        cameraService.updateCamera(updateEntity);

    }

    private void bindCameraCheck(BindCameraReq req) {
        //绑定相机id和被绑定相机id不能一致
        if (req.getId().equals(req.getCameraId())) {
            throw VehicleAccessException.PARAMS_ERROR.newInstance("绑定相机id和被绑定相机id不能一致");
        }
        //检查相机是否存在
        CameraEntity cameraEntity = checkCameraExist(req.getId());
        //检查被绑定的相机是否存在
        CameraEntity bindCameraEntity = checkCameraExist(req.getCameraId());
        //有没有绑定过相机
        if (cameraEntity.getAssociationCameraId() != null) {
            throw VehicleAccessException.PARAMS_ERROR.newInstance("不能重复绑定相机");
        }
        //判断相机是不是DETECTED识别相机
        if (!WorkModelEnum.DETECTED.name().equals(cameraEntity.getWorkMode())) {
            throw VehicleAccessException.PARAMS_ERROR.newInstance("相机必须是DETECTED识别相机");
        }
        //检查被绑定的相机是不是SNAP抓拍相机
        if (!WorkModelEnum.SNAP.name().equals(bindCameraEntity.getWorkMode())) {
            throw VehicleAccessException.PARAMS_ERROR.newInstance("被绑定相机必须是SNAP抓拍相机");
        }
    }

    @Override
    public void unbindCamera(UnBindCameraReq req) {
        unBindCameraCheck(req);
        CameraEntity updateEntity = new CameraEntity();
        updateEntity.setId(req.getId());
        cameraService.unBindCamera(updateEntity.getId());
    }

    private void unBindCameraCheck(UnBindCameraReq req) {
        //检查相机是否存在
        CameraEntity cameraEntity = checkCameraExist(req.getId());
        //判断相机是否为识别相机
        if (!WorkModelEnum.DETECTED.name().equals(cameraEntity.getWorkMode())) {
            throw VehicleAccessException.PARAMS_ERROR.newInstance("相机必须是DETECTED识别相机");
        }
        //检查相机是否绑定了相机
        if (cameraEntity.getAssociationCameraId() == null) {
            throw VehicleAccessException.PARAMS_ERROR.newInstance("未绑定抓拍相机");
        }
    }

    private CameraEntity checkCameraExist(Long id) {
        CameraEntity cameraEntity = cameraService.getCameraById(id);

        if (cameraEntity == null) {
            throw VehicleAccessException.DATA_NOT_EXIST.newInstance("相机不存在");
        }

        return cameraEntity;
    }
}
